package com.eCommerce.server.sisv.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eCommerce.corecontract.criteria.CommandeCriteria;
import com.eCommerce.corecontract.dto.ClientDTO;
import com.eCommerce.corecontract.dto.CommandeDTO;
import com.eCommerce.corecontract.dto.ComposantDTO;
import com.eCommerce.corecontract.dto.LigneCommandeDTO;
import com.eCommerce.corecontract.dto.UserDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommercePersisenceException;
import com.eCommerce.corecontract.exception.EcommerceSISVException;
import com.eCommerce.server.corebackend.dao.DAOController;
import com.eCommerce.server.corebackend.dao.IComposantDAO;

import com.eCommerce.server.corebackend.dao.ICommandeDAO;

import com.eCommerce.server.corebackend.dao.ILigneCommandeDAO;
import com.eCommerce.server.corebackend.sisv.ICommandeSISV;
import com.eCommerce.server.corebackend.util.BackEndConstants;



public class CommandeSISVImpl extends AbstractCommonSISV implements ICommandeSISV{
	
	private static Logger log =Logger.getLogger(CommandeSISVImpl.class);
	private DAOController _daoController = null;

	private ICommandeDAO _commandeDAOImpl=null;
	private ILigneCommandeDAO _ligneCommandeDAOImpl=null;
	private IComposantDAO _composantDAOImpl=null;
	public CommandeSISVImpl(DAOController daoController) throws Exception{
		super();
		_daoController = daoController;
		_commandeDAOImpl = (ICommandeDAO) _daoController.get(BackEndConstants.DAO_COMMANDE );
		_ligneCommandeDAOImpl = (ILigneCommandeDAO) _daoController.get(BackEndConstants.DAO_LIGNECOMMANDE);
		_composantDAOImpl=(IComposantDAO) _daoController.get(BackEndConstants.DAO_COMPOSANT);
		
	}
	
	public Long enregistrerCommande(CommandeDTO commandeDTO)
			throws EcommerceException {
			
		
		try
		{		
			_commandeDAOImpl.insert(commandeDTO);			
			for (LigneCommandeDTO ligne:commandeDTO.getListOfLigneCommandeDTO())
			{
					
				ComposantDTO composantDTO=new ComposantDTO();
				composantDTO.setId(ligne.getComposantDTO().getId());
				composantDTO= _composantDAOImpl.findOne(composantDTO);
				if (composantDTO!=null) 
				{
			
					if (composantDTO.getNbComposantStock() < ligne.getNbComposant()) 
						{
							Object[] arg = new Object[1];
							arg[0]= composantDTO.getLibelle();							
							EcommerceSISVException sisvException =  new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMMANDE-006",arg);
							log.error(sisvException.getMessage());
							throw sisvException;
				
						}
					else
					{
						long resteComposant= composantDTO.getNbComposantStock() - ligne.getNbComposant();
						composantDTO.setNbComposantStock(resteComposant);				
						_composantDAOImpl.update(composantDTO);
						
					}
				}
			}
			
		
		}
		catch(EcommerceException e)
		{
			throw e;
			
		
		}
		catch (Exception ex)
		{
			Object[] arg1 = new Object[1];

			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMMANDE-001", arg1, ex.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
			return commandeDTO.getId();
		}

		
		
	

	public List listerCommande() throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return _commandeDAOImpl.findAll();
		}catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMMANDE-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public List rechercherCommande(CommandeCriteria commandeCriteria)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return _commandeDAOImpl.find(commandeCriteria);
		} 
		catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMMANDE-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public CommandeDTO visualiserCommande(CommandeDTO commandeDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return _commandeDAOImpl.findOne(commandeDTO);
		} 
		catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMMANDE-003", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public void validerCommande(CommandeDTO commandeDTO) {
		// TODO Auto-generated method stub
		
		if (commandeDTO.getListOfLigneCommandeDTO() ==null ||commandeDTO.getListOfLigneCommandeDTO().size()==0)
			try {
				throw new EcommerceSISVException();
			} catch (EcommerceSISVException e) {
				Object[] arg = new Object[1];
				
				EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMMANDE-005", arg, e.getMessage());
				log.error(sisvException.getMessage());
				throw sisvException;
			}
		
		
	}

	public List rechercherCommandeDuClient(UserDTO clientDTO)
			throws EcommerceException {
		try {
			List resulList=_commandeDAOImpl.findCommandeByClient(clientDTO);
			
			
			return resulList;
			
		} 
		catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			if (clientDTO.getNom()!=null) arg[0]=clientDTO.getNom();
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMMANDE-007", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

}
